<template>
  <div>
    <breadcrumb class="breadcrumb-container"></breadcrumb>
    <el-row>
      <el-form :model="searchForm" size="small" inline>
        <el-form-item label="">
          <el-button icon="el-icon-edit" type="primary" size="small" @click="to(0,'issue')">发布通知</el-button>
        </el-form-item>
        <el-form-item label="关键字">
          <el-input v-model="searchForm.keyword"></el-input>
        </el-form-item>
        <el-form-item label="发布时间">
          <el-date-picker type="date" placeholder="开始时间" v-model="searchForm.start" value-format="yyyy-MM-dd"></el-date-picker>
          <span>-</span>
          <el-date-picker type="date" placeholder="结束时间" v-model="searchForm.end" value-format="yyyy-MM-dd"></el-date-picker>
        </el-form-item>
        <el-form-item label="">
          <el-button type="success" size="small" icon="el-icon-search" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row>
      <el-table size="small" :data="mes_data" :header-cell-style="{background:'#f2f2f2'}">
        <el-table-column align="center" label="序号" type="index"></el-table-column>
        <el-table-column align="center" label="标题" prop="title"></el-table-column>
        <el-table-column align="center" label="作者" prop="author"></el-table-column>
        <el-table-column align="center" label="发布时间" prop="created_at"></el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <span :class="scope.row.status === 0 ? 'err' : ''">{{ scope.row.status === 0 ? '发布失败': '发布成功' }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <div>
              <el-button type="text" size="small" @click="to(2,'meslist',scope.row.id)" v-if="scope.row.status === 1">查看</el-button>
              <el-button type="text" size="small" @click="to(1,'issue',scope.row.id)" v-else >重新发布</el-button>
              <el-button type="text" size="small" class="err" @click="del(scope.row.id, scope.row.title, scope.$index)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :total="page.total" layout="total, prev, pager, next, jumper"  class="fr mt20" @current-change="handleCurrentChange" :page-size="page.pageSize"></el-pagination>
    </el-row>
  </div>
</template>

<script>
import Breadcrumb from '@/components/Breadcrumb'
import { mapGetters } from 'vuex'
import { myPublishInit, myPublishPage, myPublishSearch, del } from '@/api/notice'

export default {
  name: 'message',
  components: {
    Breadcrumb
  },
  data() {
    return {
      access: 0,
      mes_data: [],
      searchForm: {
        keyword: '',
        start: '',
        end: ''
      },
      page: {
        total: 0,
        pageSize: 1
      }
    }
  },
  methods: {
    to(type, name, id) {
      if (type === 0) {
        this.$router.push({ name: name })
      } else {
        this.$router.push({ name: name, query: { id: id, type: type }})
      }
    },
    handleCurrentChange(v) {
      myPublishPage(this.id, v, this.searchForm).then(response => {
        this.mes_data = response.data.result.data
        this.page.total = response.data.result.total
      })
    },
    search() {
      myPublishSearch(this.id, this.searchForm).then(response => {
        this.mes_data = response.data.result.data
        this.page.total = response.data.result.total
      })
    },
    del(id, title, index) {
      const model = confirm('是否删除-' + title)
      if (model) {
        del(id).then(response => {
          if (response.data.errorCode === 0) {
            this.$message.success(response.data.errorMsg)
            this.mes_data.splice(index, 1)
            this.page.total === 0 ? this.page.total = 0 : this.page.total--
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
      }
    }
  },
  computed: {
    ...mapGetters([
      'id'
    ])
  },
  created() {
    myPublishInit(this.id).then(response => {
      this.mes_data = response.data.result.data
      this.page.total = response.data.result.total
      this.page.pageSize = response.data.result.per_page
    })
  }
}
</script>